import 'package:permission_handler/permission_handler.dart';

class Util{
  static Future<bool> requestCameraPermission() async {
    bool isPermission = false; // 检查是否已经授予相机权限
    PermissionStatus status = await Permission.camera.status;
    if (status.isGranted) { // 用户已经同意授予相机权限
      isPermission = true;
    } else {
      if (status.isDenied || status.isRestricted || status.isPermanentlyDenied) {
        status = await Permission.camera.request();
        if (status.isGranted) {
          isPermission = true;
        } else {
          if (status.isPermanentlyDenied) {
            openAppSettings();
            isPermission = false;
          }
        }
      }
    }
    return isPermission;
  }

  static Future<bool> requestMicrophonePermission() async {
    bool isPermission = false;
    PermissionStatus status = await Permission.microphone.status;
    if (status.isGranted) { 
      isPermission = true;
    } else {
      if (status.isDenied || status.isRestricted || status.isPermanentlyDenied) {
        status = await Permission.microphone.request();
        if (status.isGranted) {
          isPermission = true;
        } else {
          if (status.isPermanentlyDenied) {
            openAppSettings();
            isPermission = false;
          }
        }
      }
    }
    return isPermission;
  }

  static Future<bool> requestStoragePermission() async {
    bool isPermission = false;
    PermissionStatus status = await Permission.storage.status;
    if (status.isGranted) { 
      isPermission = true;
    } else {
      if (status.isDenied || status.isRestricted || status.isPermanentlyDenied) {
        status = await Permission.storage.request();
        if (status.isGranted) {
          isPermission = true;
        } else {
          if (status.isPermanentlyDenied) {
            openAppSettings();
            isPermission = false;
          }
        }
      }
    }
    return isPermission;
  }

}